import pandas as pd

from StressAna.Lib.Model import Globalvariables


def get_dev_loc():
    df_excel = pd.read_excel(rf"{Globalvariables.DOC_PATH}/2.赵楼应力计设备安装距离详情-统计稿.xls",
                             sheet_name='Sheet2', header=1)
    loc_dict_G = {}
    loc_dict_Y = {}
    min_loc_G = 9999
    min_loc_Y = 9999
    # 2月3日运顺推进度距离最近的测点30米
    _process_Y = 30
    # 2月3日轨顺推进度距离最近的测点46米
    _process_G = 46

    loc_list = df_excel['位置（米）'].to_list()
    for i, name in enumerate(df_excel['设备命名'].tolist()):
        if name[0] == 'G':
            loc_dict_G[name] = loc_list[i]
            if loc_list[i] < min_loc_G:
                min_loc_G = loc_list[i]
        if name[0] == 'Y':
            loc_dict_Y[name] = loc_list[i]
            if loc_list[i] < min_loc_Y:
                min_loc_Y = loc_list[i]
    for dev, loc_value in loc_dict_Y.items():
        loc_dict_Y[dev] = loc_value - min_loc_Y + _process_Y
    for dev, loc_value in loc_dict_G.items():
        loc_dict_G[dev] = loc_value - min_loc_G + _process_G
    return loc_dict_G, loc_dict_Y


def date(para):
    delta = pd.Timedelta(str(int(para)) + 'days')
    time = pd.to_datetime('1899-12-30') + delta
    time = time.strftime('%Y-%m-%d')
    return time


def get_day_progess():
    df_process_excel = pd.read_excel(rf"{Globalvariables.DOC_PATH}/5305工作面推进度.xlsx", header=1, skiprows=[2, 3],
                                     parse_dates=[0], date_parser=date, index_col=0)
    df_process_excel.set_index(df_process_excel.index.astype('str'), inplace=True)
    df_process_excel.iloc[0, [0, 1]] = 0  # 从2月3日开始，日推进度改为0
    day_process_G = df_process_excel['轨顺日进尺'].to_dict()
    day_process_Y = df_process_excel['运顺日进尺'].to_dict()
    return day_process_G, day_process_Y


def get_date_range(interval_dis=50):
    # 需要判断的距离间隔
    loc_dict_G, loc_dict_Y = get_dev_loc()
    day_process_G, day_process_Y = get_day_progess()
    dev_dis_dates = {}

    for dev, loc_value in loc_dict_G.items():
        dev_dis_dates[dev] = []
        all_process = 0
        for date, process in day_process_G.items():
            all_process += process
            if 0 <= loc_value - all_process <= interval_dis:
                dev_dis_dates[dev].append(date)
    for dev, loc_value in loc_dict_Y.items():
        dev_dis_dates[dev] = []
        all_process = 0
        for date, process in day_process_Y.items():
            all_process += process
            if 0 <= loc_value - all_process <= interval_dis:
                dev_dis_dates[dev].append(date)
    return dev_dis_dates


def main():
    get_date_range()


if __name__ == '__main__':
    main()
